package com.ly.hotel.view.common;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.ly.hotel.msgevent.UpdateBlacklightEvent;
import com.ly.hotel.R;
import com.ly.hotel.adaptor.HardwareInterface;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.SharedConfBlacklight;
import com.ly.hotel.common.SysConf;
import com.ly.hotel.service.BacklightService;

import de.greenrobot.event.EventBus;

public class ToolsDialog extends Dialog {
	
	public ToolsDialog(Context context) {
		super(context);
	}

	public ToolsDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private String callButtonText;
		private String jishiButtonText;
		private String xiaofeiButtonText;

		private TextView versionText;
		private OnClickListener homeButtonClickListener;
		private OnClickListener callButtonClickListener;
		private OnClickListener liuweiButtonClickListener;
		private OnClickListener langButtonClickListener;
		private OnClickListener billButtonClickListener;
		private OnClickListener jishiButtonClickListener;
		private OnClickListener xiaofeiButtonClickListener;
		private OnClickListener workBeginButtonClickListener;
		private OnClickListener workAddButtonClickListener;
		private OnClickListener workEndButtonClickListener;
		private SeekBar volume_seekBar;
		private SeekBar blacklight_seekBar;
		private int currentVolume = 0;

		// 常量（handler所使用）
		private final class handles {
			public static final int STARTIMESTRCHANG = 1;
			public static final int ENDTIMESTRCHANG = 2;
			public static final int SEEKBARCHANG = 3;
			public static final int SETVOLUME = 4;
		}


		Handler myhandler = new Handler() {
			public void handleMessage(android.os.Message msg) {
				switch (msg.what) {

				case handles.SETVOLUME:
					HardwareInterface.setVolume(currentVolume);
					break;
				}
			};
		};


		public Builder(Context context) {
			this.context = context;
		}

		public Builder setHomeButton(OnClickListener listener) {
			this.homeButtonClickListener = listener;
			return this;
		}

		public Builder setCallButton(String meButtonText, OnClickListener listener) {
			this.callButtonText = meButtonText;
			this.callButtonClickListener = listener;
			return this;
		}

		public Builder setLiuweiButton(OnClickListener listener) {
			this.liuweiButtonClickListener = listener;
			return this;
		}

		public Builder setLangButton(OnClickListener listener) {
			this.langButtonClickListener = listener;
			return this;
		}

		public void setBillButtonClickListener(OnClickListener billButtonClickListener) {
			this.billButtonClickListener = billButtonClickListener;
		}

		public void setJishiButtonClickListener(String js,OnClickListener jishiButtonClickListener) {
			this.jishiButtonText = js;
			this.jishiButtonClickListener = jishiButtonClickListener;
		}

		public void setXiaofeiButtonClickListener(String xf,OnClickListener xiaofeiButtonClickListener) {
			this.xiaofeiButtonText = xf;
			this.xiaofeiButtonClickListener = xiaofeiButtonClickListener;
		}

		public void setWorkBeginButtonClickListener(OnClickListener workBeginButtonClickListener) {
			this.workBeginButtonClickListener = workBeginButtonClickListener;
		}

		public void setWorkAddButtonClickListener(OnClickListener workAddButtonClickListener) {
			this.workAddButtonClickListener = workAddButtonClickListener;
		}

		public void setWorkEndButtonClickListener(OnClickListener workEndButtonClickListener) {
			this.workEndButtonClickListener = workEndButtonClickListener;
		}

		public ToolsDialog create() {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final ToolsDialog dialog = new ToolsDialog(context, R.style.Menu_Dialog);
			View layout = inflater.inflate(R.layout.dialog_tools_layout, null);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			versionText = layout.findViewById(R.id.softVersion);
			if(MyApplication.getApkVersion() != null){
				versionText.setText("V" + MyApplication.getApkVersion() + "  ");
			}
			versionText.setVisibility(View.VISIBLE);
			volume_seekBar = (SeekBar) layout.findViewById(R.id.volume_sebar);
			Log.i("ToolsDialog", "最大音量：" + HardwareInterface.getMaxVolume());
			currentVolume = HardwareInterface.getVolume();
			Log.i("ToolsDialog", "系统音量：" + HardwareInterface.getVolume());
			volume_seekBar.setMax(HardwareInterface.getMaxVolume());
			volume_seekBar.setProgress(currentVolume);
			volume_seekBar.setOnKeyListener(new View.OnKeyListener() {

				@Override
				public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
					// TODO Auto-generated method stub
					if(arg2.getAction() == KeyEvent.ACTION_UP){
						Log.i("ToolsDialog", "keycode=" + arg2.getKeyCode() + ",process=" + volume_seekBar.getProgress());
						if(KeyEvent.KEYCODE_DPAD_LEFT == arg2.getKeyCode()){
							if(currentVolume > 0){
								currentVolume--;
							}
						} else if(KeyEvent.KEYCODE_DPAD_RIGHT == arg2.getKeyCode()){
							if(currentVolume < HardwareInterface.getMaxVolume()){
								currentVolume++;
							}
						}

						if (myhandler != null) {
							if (myhandler.hasMessages(handles.SETVOLUME)) {
								myhandler.removeMessages(handles.SETVOLUME);
							}
							myhandler.sendEmptyMessage(handles.SETVOLUME);
						} else {
							Log.e("ToolsDialog", "msgHandler == null");
						}
					}
					return false;
				}
			});

			volume_seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
				}

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					currentVolume = progress;
					if (myhandler != null) {
						if (myhandler.hasMessages(handles.SETVOLUME)) {
							myhandler.removeMessages(handles.SETVOLUME);
						}
						myhandler.sendEmptyMessage(handles.SETVOLUME);
					} else {
						Log.e("ToolsDialog", "msgHandler == null");
					}
				}
			});

			volume_seekBar.setProgress(HardwareInterface.getVolume());
			// set the confirm button
			if (homeButtonClickListener != null) {
				final LinearLayout firstBtn = layout.findViewById(R.id.homeButton);
				firstBtn.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							homeButtonClickListener.onClick(dialog, 1);
						}
					});
				firstBtn.post(new Runnable(){
					public void run(){
						firstBtn.requestFocus();
						firstBtn.requestFocusFromTouch();
					}
				});
			} else {
				layout.findViewById(R.id.homeButton).setVisibility(View.GONE);
			}
			String enable = SysConf.extendConfGetValue(SysConf.SYS_CONF_BL_ENABLE, "0");
			if(enable.equals("1")){
				LinearLayout blacklight_layout = layout.findViewById(R.id.layout_blacklight);
				blacklight_layout.setVisibility(View.VISIBLE);
				blacklight_seekBar = layout.findViewById(R.id.volume_blacklight);
				Log.i("ToolsDialog", "当前亮度：" + SharedConfBlacklight.getAlpha());
				blacklight_seekBar.setMax(200);
				blacklight_seekBar.setProgress(SharedConfBlacklight.getAlpha());
				blacklight_seekBar.setOnKeyListener(new View.OnKeyListener() {

					@Override
					public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
						if(arg2.getAction() == KeyEvent.ACTION_UP){
							Log.i("ToolsDialog", "keycode=" + arg2.getKeyCode() + ",process=" + volume_seekBar.getProgress());
							if(KeyEvent.KEYCODE_DPAD_LEFT == arg2.getKeyCode() || KeyEvent.KEYCODE_DPAD_RIGHT == arg2.getKeyCode()){
								MyApplication.softBlacklightValue = SharedConfBlacklight.getColor(200-blacklight_seekBar.getProgress(), 0, 0, 0);
							}

							SharedConfBlacklight.setAlpha(blacklight_seekBar.getProgress());
							Log.i("ToolsDialog", "设置亮度：" + MyApplication.softBlacklightValue);

							if(BacklightService.STATE == BacklightService.ACTIVE){
								EventBus.getDefault().post(new UpdateBlacklightEvent());
					    	} else {
					    		Intent i=new Intent(context, BacklightService.class);
					    		context.startService(i);
					    	}
						}
						return false;
					}
				});
				blacklight_seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					@Override
					public void onStopTrackingTouch(SeekBar seekBar) {
						SharedConfBlacklight.setAlpha(seekBar.getProgress());
						Log.i("ToolsDialog", "设置亮度：" + MyApplication.softBlacklightValue);

						if(BacklightService.STATE == BacklightService.ACTIVE){
							EventBus.getDefault().post(new UpdateBlacklightEvent());
				    	} else {
				    		Intent i=new Intent(context, BacklightService.class);
				    		context.startService(i);
				    	}
					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar) {
					}

					@Override
					public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

						MyApplication.softBlacklightValue = SharedConfBlacklight.getColor(200-seekBar.getProgress(), 0, 0, 0);
					}
				});
			}

			if (callButtonText != null) {
				((TextView) layout.findViewById(R.id.menu_callText)).setText(callButtonText);
				if (callButtonClickListener != null) {
					layout.findViewById(R.id.callButton).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							callButtonClickListener.onClick(dialog, 3);
						}
					});
				}
			} else {
				layout.findViewById(R.id.callButton).setVisibility(View.GONE);
			}

			if (liuweiButtonClickListener != null) {
                layout.findViewById(R.id.liuweiButton).setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        liuweiButtonClickListener.onClick(dialog, 3);
                    }
                });
			} else {
				layout.findViewById(R.id.liuweiButton).setVisibility(View.GONE);
			}

			if (langButtonClickListener != null) {
                layout.findViewById(R.id.languageButton).setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        langButtonClickListener.onClick(dialog, 3);
                    }
                });
			} else {
				layout.findViewById(R.id.languageButton).setVisibility(View.GONE);
			}

			if (billButtonClickListener != null) {
				layout.findViewById(R.id.billButton).setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						billButtonClickListener.onClick(dialog, 3);
					}
				});
			} else {
				layout.findViewById(R.id.billButton).setVisibility(View.GONE);
			}

			if (jishiButtonClickListener != null) {
				if(!TextUtils.isEmpty(jishiButtonText)){
					((TextView) layout.findViewById(R.id.jishiButton_text)).setText(jishiButtonText);
				}
				layout.findViewById(R.id.jishiButton).setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						jishiButtonClickListener.onClick(dialog, 3);
					}
				});
			} else {
				layout.findViewById(R.id.jishiButton).setVisibility(View.GONE);
			}

			if (xiaofeiButtonClickListener != null) {
				if(!TextUtils.isEmpty(xiaofeiButtonText)){
					((TextView) layout.findViewById(R.id.xiaofeiButton_text)).setText(xiaofeiButtonText);
				}
				layout.findViewById(R.id.xiaofeiButton).setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						xiaofeiButtonClickListener.onClick(dialog, 3);
					}
				});
			} else {
				layout.findViewById(R.id.xiaofeiButton).setVisibility(View.GONE);
			}

			if (workBeginButtonClickListener != null) {
				layout.findViewById(R.id.workBeginButton).setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						workBeginButtonClickListener.onClick(dialog, 3);
					}
				});
			} else {
				layout.findViewById(R.id.workBeginButton).setVisibility(View.GONE);
			}

			if (workEndButtonClickListener != null) {
				layout.findViewById(R.id.workEndButton).setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						workEndButtonClickListener.onClick(dialog, 3);
					}
				});
			} else {
				layout.findViewById(R.id.workEndButton).setVisibility(View.GONE);
			}

			if (workAddButtonClickListener != null) {
				layout.findViewById(R.id.workAddButton).setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						workAddButtonClickListener.onClick(dialog, 3);
					}
				});
			} else {
				layout.findViewById(R.id.workAddButton).setVisibility(View.GONE);
			}

			dialog.setContentView(layout);
			return dialog;
		}
	}
}
